/**
 * Created by chench on 2016/8/23.
 */
$(document).keydown(function (event) {
    switch(event.keyCode)
    {
        //左上右下
        case 37:
        {
            if(moveLeft())
            {
                setTimeout("generateOneNumber()", 210);
                setTimeout("isgameover()", 300);
            }
            break;

        }
        case 38:
        {
            if(moveUp())
            {
                setTimeout("generateOneNumber()", 210);
                setTimeout("isgameover()", 300);
            }
            break;
        }
        case 39:
        {
            if(moveRight())
            {
                setTimeout("generateOneNumber()", 210);
                setTimeout("isgameover()", 300);
            }
            break;
        }
        case 40:
        {
            if(moveDown())
            {
                setTimeout("generateOneNumber()", 210);
                setTimeout("isgameover()", 300);
            }
            break;
        }
        default :
            break;
    }
});

function moveLeft()
{
    if(!canMoveLeft(board))
    {
        return false;
    }
    //在可以向左移动的前提下遍历
    //同理移动的两种情况
    for(var i=0;i<4;i++) {
        for (var j = 1; j < 4; j++) {
            if (board[i][j] != 0) {
                for (var k = 0; k < j; k++) {
                    //移动
                    if (board[i][k] == 0 && noBlokHorizontalCol(i, k, j, board)) {
                        //移动
                        showMoveAnimation(i, j, i, k);
                        //代替
                        board[i][k] = board[i][j];
                        board[i][j] = 0;

                        continue;
                    }
                    //移动相加
                    else if(board[i][k] == board[i][j] && noBlokHorizontalCol(i, k, j, board )&&!hasConflicted[i][k]) {
                        showMoveAnimation(i, j, i, k);
                        //相加
                        board[i][k] += board[i][j];
                        board[i][j] = 0;
                        //分数计算
                        score += board[i][k];
                        updateScore(score);
                        //
                        hasConflicted[i][k]=true;
                        continue;
                    }
                }
            }
        }
    }

    setTimeout("updateBoardView();",200);
    return true;
}


function moveUp()
{
    if(!canMoveUp(board))
    {
        return false;
    }
    for(var i=1;i<4;i++) {
        for (var j = 0; j < 4; j++) {
            if (board[i][j] != 0) {
                for (var k = 0; k < i; k++) {
                    if (board[k][j] == 0 && noBlokHorizontalRow(k, i, j, board)) {
                        showMoveAnimation(i, j, k, j);
                        board[k][j] = board[i][j];
                        board[i][j] = 0;

                        continue;
                    }
                    else if (board[k][j] == board[i][j] && noBlokHorizontalRow(k, i, j, board)&& !hasConflicted[k][j]){
                        showMoveAnimation(i, j, k, j);
                        board[k][j] += board[i][j];
                        board[i][j] = 0;
                        score += board[k][j];
                        updateScore(score);
                        //
                        hasConflicted[k][j]=true;
                        continue;
                    }
                }
            }
        }
    }

    setTimeout("updateBoardView();",200);
    return true;
}

function moveRight()
{
    if(!canMoveRight(board))
    {
        return false;
    }
    //遍历
    for (var i = 0; i < 4; i++) {
        for (var j = 2; j >=0; j--) {
            if (board[i][j] != 0) {
                for (var k = 3; k > j; k--) {
                    //移动
                    if (board[i][k] == 0 && noBlokHorizontalCol(i, j, k, board)) {
                        showMoveAnimation(i, j, i, k);
                        board[i][k] = board[i][j];
                        board[i][j] = 0;
                        continue;
                    }
                    //移动相加
                    else if(board[i][k] == board[i][j] && noBlokHorizontalCol(i, j, k, board) && !hasConflicted[i][k]) {
                        showMoveAnimation(i, j, i, k);
                        board[i][k] += board[i][j];
                        board[i][j] = 0;
                        score += board[i][k];
                        updateScore(score);

                        hasConflicted[i][k]=true;
                        continue;
                    }
                }
            }
        }
    }

    setTimeout("updateBoardView();",200);
    return true;
}

function moveDown()
{
    if(!canMoveDown(board))
    {
        return false;
    }
    for (var i = 2; i >= 0; i--) {
        for (var j = 0; j < 4; j++) {
            if (board[i][j] != 0) {
                for (var k = 3; k > i; k--) {
                    //移动
                    if (board[k][j] == 0 && noBlokHorizontalRow(i, k, j, board)) {
                        showMoveAnimation(i, j, k, j);
                        board[k][j] = board[i][j];
                        board[i][j] = 0;
                        continue;
                    }
                    //移动相加
                    else if(board[k][j] == board[i][j] && noBlokHorizontalRow(i, k, j, board)&&!hasConflicted[k][j]) {
                        showMoveAnimation(i, j, k, j);
                        board[k][j] += board[i][j];
                        board[i][j] = 0;
                        score += board[k][j];
                        updateScore(score);
                        //
                        hasConflicted[k][j]=true;
                        continue;
                    }
                }
            }
        }
    }

    setTimeout("updateBoardView();",200);
    return true;
}


function isgameover() {
    if (nospace(board) && nomove(board)) {
        gameover();
    }
}

function gameover() {
    alert("Game Over");
    $("#grid-container").append("<div id='gameover' class='gameover'><p>本次得分</p><span>" + score + "</span><a href='javascript:restartgame();' id='restartgamebutton'>Restart</a></div>");
    var gameover = $("#gameover");
    gameover.css("width", "500px");
    gameover.css("height", "500px");
    gameover.css("background-color", "rgba(0, 0, 0, 0.5)");
}



